package com.yangrong.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

	public static void main(String[] args) {
		// 固定线程池
//		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		// 动态线程池
//		ExecutorService threadPool = Executors.newCachedThreadPool();
		// 单一线程池（线程死掉后会重新启动一个线程）
		ExecutorService threadPool = Executors.newSingleThreadExecutor();
		for (int i = 1; i <= 10; i++) {
			final int task = i;
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					for (int i = 1; i <= 10; i++) {
						try {
							Thread.sleep(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName() + " is looping of " + i + " for task of " + task);
					}
				}
			});
		}
		threadPool.shutdown();
		
//		Executors.newScheduledThreadPool(3).schedule(new Runnable() {
//			@Override
//			public void run() {
//				System.out.println("Bombing!");
//			}
//		}, 10, TimeUnit.SECONDS);
		
		Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				System.out.println("Bombing!");
			}
		}, 6, 2, TimeUnit.SECONDS);
	}

}
